/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn2;

import java.io.*;
import java.util.*;
/**
 *
 * @author Mathias
 */
public class FileRepository {

    private String repoDir;

    public FileRepository(String path) {
        this.repoDir = path;
    }


    public String getRepoDir() {
        return repoDir;
    }

    public void setRepoDir(String aRepoDir) {
        repoDir = aRepoDir;
    }

    public void getFileList() {

    }

    public ArrayList findFile(String pattern) {
        File myFile = new File(this.getRepoDir());

        ArrayList findings = new ArrayList();
        String[] fileList = myFile.list();      

        for (int i = 0; i < fileList.length; i++) {
            if (fileList[i].contains(pattern)) {
                findings.add(fileList[i]);
            }            
        }

        return findings;
    }

    public byte[] getFileByteArray(String filename) throws Exception {

        File myFile = new File(getRepoDir() + filename);
        long fileLength = myFile.length();

        // Create the byte array to hold the data
        byte[] bytes = new byte[(int)fileLength];

        InputStream is = new FileInputStream(myFile);

        try {

            // Read in the bytes
            int offset = 0;
            int numRead = 0;
            while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
                  offset += numRead;
            }

            // Ensure all the bytes have been read in
            if (offset < bytes.length) {
                
                throw new IOException("[ClientHandlerThread] Could not completely read file " + myFile.getName());
            }

            // Close the input stream and return bytes
            is.close();

        } catch (Exception e) {
            try {
                is.close();
            } catch (Exception e1) {
                // ignore
            }
            System.out.println("[FileRepository] exception caught while reading file " + e.toString());
            throw e;
        }

        return bytes;
    }

    public void storeFileByteArray(String filename, byte[] b, long size, boolean append) {

        FileOutputStream fos = null;
        try {
            File myFile = new File(this.repoDir + filename);
        
            fos = new FileOutputStream(this.repoDir + filename, append);
           

            System.out.println("[FileRepository] writing to file " + this.repoDir + filename + "...");
            
            fos.write(b);
            
            System.out.println("[FileRepository] finished writing to file " + this.repoDir + filename);

            fos.close();
            
        } catch (Exception e) {
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e1) {
                    // ignore
                }                
            }
            System.out.println("[FileRepository] exception while writing to file " + e.toString());
        }
        
        
    }
}
